function fname = rt_writedat(fnameroot, t, P, T, C, S, D, DOS, DO)
    % Write to file real-time raw and calculated data, preceded by the 
    % place-indicating Header that is assigned below to the variable "template".
    %
    % (Typical) Usage:
    %
    % Typical usage is that this function is called by rt_driver; 
    % typically, the user will not call this function directly
    %
    global t_interval sitedata
    template = {... % Standard Header required by   ; must contain 60 lines, i.e.,
                    % assure this if modifying
    '# **************************************************************************\n';
    '# ------------------ PROVISIONAL DATA SUBJECT TO REVISION ------------------\n';
    '# **************************************************************************\n';
    '# Continuous Real-time Marine Water Data from Monitoring Stations (Moorings)\n';
    '# MOORING_COTS Database Export:	%s to %s UTC\n';
    '# --------------------------------------------------------------------------\n';
    '# \n';
    '# ** STATION INFORMATION\n';
    '# \n';
    '# Name:        %s \n';
    '# Location:    %s \n';
    '# Description: %s, %s \n';	
    '# Latitude:    %6.3f deg \n';		 
    '# Longitude:   %6.3f deg \n';	
    '# MLLW:        %6.3f m \n';	
    '# \n';
    '# --------------------------------------------------------------------------\n';
    '# \n';
    '# ** REAL-TIME DATA DICTIONARY\n';
    '# \n';
    '# All time stamps are in Coordinated Universal Time (UTC).\n';
    '# \n';
    '# Values for missing/flagged data have been set to -9999.\n';
    '# \n';
    strcat('# - Columns: UTC, Pressure, Temperature, Conductivity, Salinity, Density,',...
    ' DissolvedOxygenSat, DissolvedOxygen\n');
    '# \n';
    '# UTC (ISO 8601)	''yyyymmddTHHMMSS''\n';
    '# Pressure (db)\n';
    '# Temperature (deg C)\n';
    '# Conductivity (S/m)\n';
    '# Salinity (psu)\n';
    '# Density (Sigma-t, kg/m3)\n';
    '# DissolvedOxygenSat (mg/L)\n';
    '# DissolvedOxygen (mg/L)\n';
    '# \n';
    '# --------------------------------------------------------------------------\n';
    '# \n';
    '# ** DATA DISTRIBUTION\n';
    '# \n';
    '# This dataset is distributed by the Washington State Department of Ecology.\n';
    '# \n';
    '# Real-time Data:		ftp://www.ecy.wa.gov/eap/Mooring_Raw\n';
    '# QAQC''d Data:		ftp://www.ecy.wa.gov/eap/Mooring_Reviewed\n';
    '# \n';
    '# --------------------------------------------------------------------------\n';
    '# \n';
    '# ** CONTACT INFORMATION\n';
    '# \n';
    '# Washington State Department of Ecology\n';
    '# Environmental Assessment Program (EAP) - Marine Waters Unit (Moorings)\n';
    '# \n';
    '# Ecology Home:		http://www.ecy.wa.gov/ecyhome.html\n';
    '# EAP Home:			http://www.ecy.wa.gov/programs/eap/index.html\n';
    '# Marine Waters Home:	http://www.ecy.wa.gov/programs/eap/mar_wat/mwm_intr.html\n';
    '# Moorings Home:		http://www.ecy.wa.gov/programs/eap/mar_wat/moorings.html\n';
    '# \n';
    '# --------------------------------------------------------------------------\n';
    '# \n';
    '# \n';
    '# ** END HEADER INFORMATION: LINE 60\n'};

    % Open output file
    fname = [fnameroot '.txt'];
    [out, msg] = fopen(fname, 'wt');
    if isempty(msg) % if successful
        % Write Header
        fprintf(out, strcat(template{:}), datestr(t_interval(1), 'dd-mmm-yyyy HH:MM'),...
                                          datestr(t_interval(2), 'dd-mmm-yyyy HH:MM'),...
                                          sitedata{:});

        % Write column labels
        if ~isempty(T)
            [~, col_heds] = strtok(template{25}, 'U');
            col_heds = regexp(col_heds, ',', 'split');
            fprintf(out, '%s,', col_heds{1});
            fprintf(out, '%s,', col_heds{2:7});
            fprintf(out, '%s\n', col_heds{8}(1:end-2));

            % Write data
            for i = 1:length(t)
                fprintf(out, '%s', datestr(t(i), 30));
                NaN2n9999(out, P(i));
                NaN2n9999(out, T(i));
                NaN2n9999(out, C(i));
                NaN2n9999(out, S(i));
                NaN2n9999(out, D(i));
                NaN2n9999(out, DOS(i));
                if isempty(DO);
                    NaN2n9999(out, NaN);
                else
                    NaN2n9999(out, DO(i));
                end
                fprintf(out, '\n');
            end
        end
        fclose(out); % Close the file
    else
        fname = msg;
    end
end

function NaN2n9999(out, x)
    % Ad hoc function to convert NaN's in source data to -9999's for the ascii output
    if isnan(x)
        fprintf(out, ', %6.0f', -9999);
    else
        fprintf(out, ', %6.3f', x);
    end
end